package net.pm.core.web.user;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.Pager;
import net.pm.common.toolkit.SHAEncryptUtils;
import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.cache.SessionStorer;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.service.pmuser.PMDeptServicer;
import net.pm.core.service.pmuser.PMUserServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/user/")
public class UserController extends BaseController {

	@Resource(name = "PMUserServicer")
	private PMUserServicer userServicer;
	@Resource(name="PMDeptServicer")
	private PMDeptServicer deptServicer;
	
	//重定身到用户管理页面
	private static final String REDIRECT_USER_MANAGE = "redirect:/user/manage.htm";

	/**
	 * 去到用户管理页面
	 * @return
	 */
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public String manage() {
		return "user/user_manage";
	}
	
	/**
	 * 自动转到默认页
	 * @return
	 * */
	@RequestMapping(value = "page", method = RequestMethod.GET)
	public ModelAndView page() {
		return page(DevConstant.PAGE_DEFAULT_SIZE,DevConstant.PAGE_DEFAULT_NO);
	}
	
	/**
	 *  部门用户管理页面
	 * @return
	 */
	@RequestMapping(value="page/{pagesize}/{pageno}",method=RequestMethod.GET)
	public ModelAndView page(@PathVariable("pagesize")Integer pagesize,@PathVariable("pageno")Integer pageno){
		ModelAndView mav = new ModelAndView("user/user_page");
		Integer deptId = super.obtainCurrentDept().getId();
		Pager<Pmuser> pager = userServicer.findUserPagerByOwner(deptId,pagesize, pageno);
		
		mav.addObject("pager", pager);
		//让页面支持静态方法访问
		mav.addObject("SessionStorer",
				super.useStaticPacker(SessionStorer.class.getName()));
		return mav;
	}
	
	/**
	 * 去到创建用户页面
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_PMUSER)
	@RequestMapping(value="create",method=RequestMethod.GET)
	public ModelAndView userCreate(){
		ModelAndView mav = new ModelAndView("user/user_create");
		
		Pmuser user = super.obtainCurrentUser();
		//如果当前是管理员,才可以设置新增员工的部门
		if(user.getIsAdmin()){
			mav.addObject("depts", deptServicer.findAll());
		}
		return mav;
	}
	
	/**
	 * 创建添加一个用户
	 * @param user
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_PMUSER)
	@RequestMapping(value="create",method=RequestMethod.POST)
	public ModelAndView userCreate(Pmuser user){
		
		if(userServicer.createUser(user) == PMUserServicer.create_user_success){
			super.setSessionMessage("创建新用户成功!");
		}else{
			super.setSessionMessage("创建新用户失败，请稍候重试!");
		}
		
		return userCreate();
	}
	
	/**
	 * 激活用户
	 * @param userId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.DISABLE_PMUSER)
	@RequestMapping(value="enable",method=RequestMethod.PUT)
	public String enable(Long userId){
		if(userServicer.enableUser(userId).equals(PMUserServicer.enable_user_success)){
			super.setSessionMessage("启用用户成功!");
		}else{
			super.setSessionMessage("启用用户失败!");
		}
		return REDIRECT_USER_MANAGE;
	}
	
	/**禁用用户
	 * @param userId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.DISABLE_PMUSER)
	@RequestMapping(value="disable",method=RequestMethod.PUT)
	public String disable(Long userId){
		Long current_user_id = super.obtainCurrentUser().getId();
		//不能禁用自己
		if(current_user_id.equals(userId)){
			super.setSessionMessage("不能禁用自己!");
			return REDIRECT_USER_MANAGE;
		}
		Pmuser tempUser = userServicer.findEntityById(userId);
		if(tempUser.getIsAdmin()){
			super.setSessionMessage("不能禁用管理员!");
			return REDIRECT_USER_MANAGE;
		}
		
		if(userServicer.disableUser(userId).equals(PMUserServicer.disable_user_success)){
			super.setSessionMessage("禁用用户成功!");
			//禁用成功,将用户踢出
			Pmuser disableUser = userServicer.findEntityById(userId);
			SessionStorer.removeSessionMap(disableUser.getPmdept().getId(), userId);
			
		}else{
			super.setSessionMessage("禁用用户失败!");
		}
		return REDIRECT_USER_MANAGE;
	}
	
	
	/**
	 * 去到密码更新页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "passwd/modify", method = RequestMethod.GET)
	public String passwdModify() {
		return "user/user_passwd_modify";
	}

	/**
	 * 更新密码
	 * 
	 * @param oldpasswd
	 *            旧密码
	 * @param newpasswd
	 *            新密码
	 * @return
	 */
	@RequestMapping(value = "passwd/modify", method = RequestMethod.PUT)
	public String passwdModify(String oldpasswd, String newpasswd) {
		Long userid = super.obtainCurrentUser().getId();
		// 当前用户的密码
		String ownerpasswd = userServicer.findEntityById(userid).getPassWord();
		// Request传过来的旧密码
		String oldpassword_encry = SHAEncryptUtils.getDigestOfString(oldpasswd);

		if (ownerpasswd.equals(oldpassword_encry)) {
			userServicer.modifyPasswd(
					SHAEncryptUtils.getDigestOfString(newpasswd), userid);
			super.setSessionMessage("更新密码成功!");
		} else {
			super.setSessionMessage("旧密码输入错误!");
		}

		return "redirect:/user/passwd/modify.htm";
	}
	
	/**
	 * 重置用户密码
	 * @param passwd
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="passwd/reset",method=RequestMethod.PUT)
	public String passwdReset(String passwd,Long userId){
		if (userServicer.resetPasswd(passwd, userId).equals(
				PMUserServicer.reset_passwd_success)) {
			super.setSessionMessage("重置密码成功！");
		}else{
			super.setSessionMessage("重置密码失败!");
		}
		
		return REDIRECT_USER_MANAGE;
	}
	
	/**
	 * 查看用户详细信息
	 * @return
	 */
	@RequestMapping(value="info",method=RequestMethod.GET)
	public ModelAndView info(Long userId){
		ModelAndView mav = new ModelAndView("user/user_info");
		Pmuser tempuser = userServicer.findEntityById(userId);
		mav.addObject("user", tempuser);
		return mav;
	}
	
	@RequestMapping(value="info/json",method=RequestMethod.GET)
	@ResponseBody
	public Pmuser infoJson(Long userId){
		return userServicer.findEntityById(userId);
	}
	
	
	
	/**
	 * 去到更新用户信息页面
	 * @param user
	 * @return
	 */
	@RequestMapping(value="info/modify",method=RequestMethod.GET)
	public String infoModify(){
		return "user/user_info_modify";
	}
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value="info/modify",method=RequestMethod.PUT)
	public ModelAndView infoModify(Pmuser user){
		ModelAndView mav = new ModelAndView("user/user_info_modify");
		
		return mav;
	}
	

}
